package com.gec.controller;

import com.alibaba.fastjson.JSONObject;
import com.gec.domain.Dept;
import com.gec.domain.PageBean;
import com.gec.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author Aniki
 * @date 2021-12-15
 */
@Controller
@RequestMapping("/Dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/viewList")
    public String viewList(){
        return "dept/viewList";
    }


    @RequestMapping("/viewAdd")
    public String ViewAddDept(){
        return "dept/addDept";
    }

    @RequestMapping(value = "/jsonList",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String jsonList(@RequestParam Map<String,String> data,
                           @RequestParam(value = "page", required = true, defaultValue = "1")Integer page,
                           @RequestParam(value = "limit", required = true, defaultValue = "7")Integer limit){
        PageBean pageBean = null;
        String respTxt = "";
        try {
           pageBean = deptService.getDeptList(page, limit, data);
            return packJSON(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return setErrorMsg(e);
        }
    }

    @RequestMapping(value = "/delete",produces  = "text/html;charset=UTF-8" )
    @ResponseBody
    public  String delDept(String id) {
        String respTxt = "";
        JSONObject jsonObject = new JSONObject();
        try {
            deptService.delDept(id);
            jsonObject.put("result","success");
            respTxt = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }

    @RequestMapping(value = "/save",produces  = "text/html;charset=UTF-8" )
    @ResponseBody
    public  String saveDept(Dept dept) {
        String respTxt = "";
        JSONObject jsonObject = new JSONObject();
        try {
            deptService.saveDept(dept);
            jsonObject.put("result","success");
            respTxt = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }

    @RequestMapping(value = "/update",produces  = "text/html;charset=UTF-8" )
    @ResponseBody
    public  String updateDept(Dept dept) {
        String respTxt = "";
        JSONObject jsonObject = new JSONObject();
        try {
            deptService.updateDept(dept);
            jsonObject.put("result","success");
            respTxt = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }

    @RequestMapping(value = "/getDept",produces  = "text/html;charset=UTF-8" )
    @ResponseBody
    public  String getDept(@RequestParam("id") String id) {
        String respTxt = "";
        JSONObject jsonObject = new JSONObject();
        try {
            Dept dept = deptService.getDept(id);
            jsonObject.put("result","success");
            jsonObject.put("dept",dept);
            respTxt = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }
}